using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using ctsu.Admin.Forms;
using ctsu.BaseForms;
using ctsu.Security;
using ctsu.Shared;

namespace ctsu.Admin.BusLogic
{
	public class BLFormCommonUserInfoImpl : BLFormBaseImpl
	{
		private TextBox _rePassword;
		private TextBox _password;

		public BLFormCommonUserInfoImpl(FormMain formMain, FormBase form, FormInfoBase formInfo, DataTable tableSchema, Permission formSecurity, Hashtable configData)
			: base(formMain, form, formInfo, tableSchema, formSecurity, configData)
		{

		}

		public override void GenericClick(Control sender)
		{
			Console.WriteLine("GenericClick override");
		}

		public override bool Initialize()
		{
			this._rePassword = (TextBox)Forms.Utils.GetControlByName("RePassword", this._form.Controls);
			this._password = (TextBox)Forms.Utils.GetControlByName("Password", this._form.Controls);

			if (base.Initialize())
			{
				this._form.btnSearch.Visible = false;
				this._form.btnNew.Visible = false;
				this._form.btnRemove.Visible = false;
				this._form.btnGrid.Visible = false;
				this._form.chkGridTracking.Visible = false;

				base.GetData(LoginForm.ApplicationSecurity.ApplicationUser.ID);
				SetFormMode(EditMode.Read);
				this._rePassword.Text = this._password.Text;

				return true;
			}
			return false;
		}

		protected override void SetControlsEnabled(bool enable)
		{
			base.SetControlsEnabled(enable);
			this._rePassword.Enabled = enable;
		}

		public override bool Save()
		{
			bool retVal = base.Save();
			if (retVal)
			{
				MessageBox.Show("You must log off and log back on for the changes to take effect.", "Save Confirmation",
					MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			return retVal;
		}

		private bool VerifyPassword(Control ctl)
		{
			if (!VerifyText(ctl))
				return false;

			FieldContext fc = (FieldContext)ctl.Tag;
			if (!fc.IsColumnChanged(fc.Column.ColumnName))
				return true;

			if (ctl.Text != this._rePassword.Text)
			{
				fc.ErrorMessage = "Password and re-write password do not match";
				return false;
			}
			return true;
		}

		protected override void _vcManager_OnAfterHookEvents()
		{
			base._vcManager_OnAfterHookEvents();

			//this gives you the form is dirty functionality			
			this._rePassword.TextChanged += new EventHandler(Central_TextChanged);
			this._rePassword.Validating += new CancelEventHandler(RePassword_Validating);

			//hook password text changed			
			//this._password.TextChanged +=new EventHandler(Password_TextChanged);		
		}

		private void RePassword_Validating(object sender, CancelEventArgs e)
		{
			bool b = VerifyPassword(this._password);
			this.CheckErrorStatus(b, (FieldContext)this._password.Tag);
		}

		//		private void Password_TextChanged(object sender, EventArgs e)
		//		{
		//			Control ctl = Forms.Utils.GetControlByName("RePassword", this._form.Controls);
		//			ctl.Enabled = true;
		//		}


	}
}
